package com.atguigu.srb.oss.controller;

import com.atguigu.srb.common.exception.BusinessException;
import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.common.result.Result;
import com.atguigu.srb.oss.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author lsyandwh
 * @create 2022-12-09 11:27
 */
@Api(tags = "文件存储")
@RestController
@RequestMapping("/api/oss")
//@CrossOrigin
public class FileController {

    @Autowired
    private FileService fileService;

    @ApiOperation("文件上传")
    @PostMapping("/put")
    public Result putFile(@RequestParam MultipartFile file,@RequestParam String module){

        if(null == file){
            throw new BusinessException(ResponseEnum.UPLOAD_ERROR);
        }
        String url = fileService.upload(file,module);

        return Result.ok().data("url", url);
    }

    @ApiOperation("删除文件")
    @DeleteMapping("/delete")
    public Result removeFile(@RequestParam String url){

        fileService.removeFile(url);

        return Result.ok();
    }

}
